package com.ruoyi.hotel.controller;

import java.util.Arrays;

import com.ruoyi.hotel.domain.HmCheckin;
import com.ruoyi.hotel.domain.HmServiceRequest;
import com.ruoyi.hotel.domain.HmTask;
import com.ruoyi.hotel.domain.bo.HmServiceRequestBo;
import com.ruoyi.hotel.domain.bo.HmTaskBo;
import com.ruoyi.hotel.mapper.HmServiceRequestMapper;
import com.ruoyi.hotel.mapper.HmTaskMapper;
import com.ruoyi.hotel.service.IHmCheckinService;
import com.ruoyi.hotel.service.IHmTaskService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hotel.domain.vo.HmServiceRequestVo;
import com.ruoyi.hotel.service.IHmServiceRequestService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 服务需求操作
 *
 * @date 2023-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hotel/serviceRequest")
public class HmServiceRequestController extends BaseController {

    private final IHmServiceRequestService iHmServiceRequestService;
    private final IHmCheckinService iHmCheckinService;
    private final IHmTaskService iHmTaskService;

    /**
     * 查询服务需求列表
     */
    @SaCheckPermission("hotel:serviceRequest:list")
    @GetMapping("/list")
    public TableDataInfo<HmServiceRequestVo> list(HmServiceRequestBo bo, PageQuery pageQuery) {

        return iHmServiceRequestService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取服务需求细信息
     *
     * @param serviceRequestId 主键
     */
    @SaCheckPermission("hotel:serviceRequest:query")
    @GetMapping("/{serviceRequestId}")
    public R<HmServiceRequestVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long serviceRequestId) {
        return R.ok(iHmServiceRequestService.queryById(serviceRequestId));
    }



    /**
     * 修改服务需求
     */
    @SaCheckPermission("hotel:serviceRequest:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/update")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmServiceRequestBo bo) {

        //如果是取消(quit)或不可行(not able)，就应也把其下面的任务也取消(cancel)
        if(bo.getIsDone().equals("q")||bo.getIsDone().equals("n")) {
            Long id = bo.getServiceRequestId();
            HmTaskBo hmTask = new HmTaskBo();
            hmTask.setServiceRequestId(id);
            hmTask.setIsDone("c");
            iHmTaskService.updateByBo(hmTask);

        }

        return toAjax(iHmServiceRequestService.updateByBo(bo));
    }

    /**
     * 删除服务需求
     *
     * @param serviceRequestIds 主键串
     */
    @SaCheckPermission("hotel:serviceRequest:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{serviceRequestIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] serviceRequestIds) {
        return toAjax(iHmServiceRequestService.deleteWithValidByIds(Arrays.asList(serviceRequestIds), true));
    }


    /**
     * 新增服务需求(酒店方面提出)
     */
    @RepeatSubmit()
    @RequestMapping("/serviceRequest")
    public R<Void> serviceRequest(@RequestBody HmServiceRequestBo request){

        iHmServiceRequestService.insertByBo(request);

        return null;
    }


    /**
     * 获取个人的服务需求列表
     *
     */
    @RequestMapping("/getListNow")
    public TableDataInfo<HmServiceRequest> getListNow() {

        HmCheckin hmCheckin=iHmCheckinService.getCheckinNow(getUserId());

        if(hmCheckin==null){
            return null;
        }

        return TableDataInfo.build(iHmServiceRequestService.getListByCheckin(hmCheckin));

    }

    /**
     * 新增服务需求(顾客方面提出)
     */
    @RequestMapping("/addService")
    public R<Void> addService(@RequestBody HmServiceRequestBo hmServiceRequest){
        Long id=getUserId();
        HmCheckin hmCheckin=iHmCheckinService.getCheckinNow(id);

        if(hmCheckin==null){
            return toAjax(false);
        }

        hmServiceRequest.setCheckinId(hmCheckin.getCheckinId());

        hmServiceRequest.setIsDone("w");

        iHmServiceRequestService.insertByBo(hmServiceRequest);

        return toAjax(true);
    }


    @RequestMapping("/cancelMyRequest")
    public void cancelMyRequest(@RequestBody HmServiceRequestBo hmServiceRequest){

        HmCheckin hmCheckin=iHmCheckinService.getCheckinNow(getUserId());

        hmServiceRequest.setCheckinId(hmCheckin.getCheckinId());

        hmServiceRequest.setIsDone("q");

        iHmServiceRequestService.updateByBo(hmServiceRequest);

        //取消后把相应的任务也取消
        Long id = hmServiceRequest.getServiceRequestId();
        HmTaskBo hmTask = new HmTaskBo();
        hmTask.setServiceRequestId(id);
        hmTask.setIsDone("c");
        iHmTaskService.updateByBo(hmTask);


    }








}
